package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ConfigMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class Log extends BaseRepair {

    public Log(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }
    //触发礼包一条龙三个日志
    public void go1(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog1("2020-10-21","2020-11-14");
        DataMgr.addData(listMap);
    }
    public void go2(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog1("2020-10-23","2020-10-25");
        DataMgr.addData(listMap);
    }
    public void go3(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog1("2020-10-30","2020-10-04");
        DataMgr.addData(listMap);
    }
    public void go4(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog1("2020-10-25","2020-10-30");
        DataMgr.addData(listMap);
    }
    public void go5(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog1("2020-11-10","2020-11-12");
        DataMgr.addData(listMap);
    }
    public void go6(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog1("2020-12-18","2020-12-21");
        DataMgr.addData(listMap);
    }

    public void go21(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog2("2020-10-21","2020-11-13");
        DataMgr.addData(listMap);
    }

    private List<Map<String, String>> getLog1(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * " +
                " from t_l_buy " +
                " where `time` > ? AND `time` < ? AND shopType = 12";
        // and shopType in(109,)
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("mallId",checkNullToEmpty(rs.getString("mallId")));
                temp.put("time",checkNullToEmpty(rs.getString("time")));
                temp.put("price",checkNullToEmpty(rs.getString("price")));
                temp.put("shopType",checkNullToEmpty(rs.getString("shopType")));
                temp.put("goods",checkNullToEmpty(rs.getString("goods")));
                temp.put("mallName", ConfigMgr.getMallName(Integer.valueOf(temp.get("mallId"))));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLog2(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * " +
                " from t_l_buy " +
                " where `time` > ? AND `time` < ?";
        // and shopType in(109,)
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("mallId",checkNullToEmpty(rs.getString("mallId")));
                temp.put("time",checkNullToEmpty(rs.getString("time")));
                temp.put("price",checkNullToEmpty(rs.getString("price")));
                temp.put("shopType",checkNullToEmpty(rs.getString("shopType")));
                temp.put("goods",checkNullToEmpty(rs.getString("goods")));
                temp.put("mallName", ConfigMgr.getMallName(Integer.valueOf(temp.get("mallId"))));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }
}
